package com.yuunhe.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.utils.MyBatisUtil;
import com.yuunhe.dao.UserDao;
import com.yuunhe.entity.User;

public class UserDaoImpl implements UserDao {
	/**
	 * 得到用户列表
	 */
	public List<User> getAll() {
		SqlSession session = null;
		try {
			session = MyBatisUtil.getSession();
			List<User> list = session.selectList("com.yuunhe.dao.UserDao.getAll");
			return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} 
	}
	/**
	 * 增加用户
	 */
	public void addUser(User user) {
		SqlSession session = null;
		try {
			session = MyBatisUtil.getSession();
			session.insert("com.yuunhe.dao.UserDao.addUser", user);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSession();
		}
		

	}
	/**
	 * 删除用户
	 */
	public void deleteUser(int id) {
		SqlSession session = null;
		try {
			session = MyBatisUtil.getSession();
			session.delete("com.yuunhe.dao.UserDao.deleteUser", id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSession();
		}
	}
	/**
	 * 更改用户
	 */
	public void updateUser(User user) {
		SqlSession session = null;
		try {
			session = MyBatisUtil.getSession();
			session.update("com.yuunhe.dao.UserDao.updateUser", user);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSession();
		}

	}
	/**
	 * 查询用户根据id
	 */
	public User findById(int id) {
		SqlSession session = null;
		try {
			session = MyBatisUtil.getSession();
			User user = session.selectOne("com.yuunhe.dao.UserDao.findById", id);
			return user;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);

		} finally {
			MyBatisUtil.closeSession();
		}
	}

}
